package com.neusoft.lohas.modules.shop.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.persistence.StatusBean;
import com.neusoft.lohas.common.utils.DateUtils;
import com.neusoft.lohas.common.utils.FloatFormat;
import com.neusoft.lohas.common.web.BaseShopController;
import com.neusoft.lohas.modules.ivm.entity.merchant.DeliveryFee;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;
import com.neusoft.lohas.modules.ivm.entity.order.ShoppingCart;
import com.neusoft.lohas.modules.ivm.service.merchant.DeliveryFeeService;
import com.neusoft.lohas.modules.shop.service.ShopService;
import com.neusoft.lohas.modules.shop.utils.ShopUtils;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${frontPath}/shops/cart")
public class CartController extends BaseShopController {

	@Autowired
	private ShopService shopService;
	@Autowired
	private DeliveryFeeService deliveryFeeService;
	
	@RequestMapping(value = "")
	public String showBusinessIndex(
			String sid,
			String mobile,
			@RequestParam(value="username", required=false, defaultValue="")String username,
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			Model model, RedirectAttributes redirectAttributes){
		//处理mobile参数
		if(StringUtils.isNotBlank(mobile))mobile = mobile.split(",")[0];
		Merchant merchant = ShopUtils.getMerchantById(sid);
		User user = UserUtils.getShopUser(mobile);
		//判断是否是新用户,0:新用户；1:老用户
		String userType = "0";
		if(user != null){
			userType = user.getType();
		}
		List<ShoppingCart> list = shopService.findShoppingCarts(sid, user.getId());
		//定义保存购买总数量和总金额变量
		int totalNumber=0;
		double totalAmount=0.00;
		boolean hasPrize = false;
		boolean gift = false;//商品是否为新用户礼包
		for (ShoppingCart cart : list) {
			totalNumber += cart.getNumber();
			totalAmount += cart.getAmount();
			if (cart.getType()==1) {
				hasPrize = true;
			}
			if(cart.getProduct().getGift().equals("1")){
				gift = true;
			}
		}
		//获取派送费信息
		DeliveryFee deliveryFee = deliveryFeeService.findByMerchantId(merchant.getId());
		if(StringUtils.isBlank(deliveryFee.getOpenState()) || deliveryFee.getOpenState().equals("0")){
			model.addAttribute("fee", 0);
			model.addAttribute("lowFee", 0);
		}else{
			Date nowDate = new Date();
			if(nowDate.getTime()>=deliveryFee.getBeginDate().getTime() && nowDate.getTime()<=deliveryFee.getEndDate().getTime()){
        		model.addAttribute("fee", deliveryFee.getAmount());
        		model.addAttribute("lowFee", deliveryFee.getLowFee());
        	}else{
        		model.addAttribute("fee", 0);
            	model.addAttribute("lowFee", 0);
        	}
		}
		model.addAttribute("list", list);
		model.addAttribute("totalNumber", totalNumber);
		model.addAttribute("totalAmount", FloatFormat.m2(totalAmount));
		model.addAttribute("merchant", merchant);
		model.addAttribute("mobile", mobile);
		model.addAttribute("appId", appId);
		model.addAttribute("hasPrize", hasPrize);
		model.addAttribute("gift", gift);
		model.addAttribute("userType", userType);
		return "mobile/shop/cart/cart";
	}
	
	/**
	 * 获取最新购物车商品数量
	 * @param sid
	 * @param mobile
	 * @param username
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "getCount")
	public @ResponseBody Object getCount(
			String sid,
			String mobile,
			@RequestParam(value="username", required=false, defaultValue="")String username,
			Model model, RedirectAttributes redirectAttributes){
		//处理mobile参数
		if(StringUtils.isNotBlank(mobile))mobile = mobile.split(",")[0];
		User user = UserUtils.getShopUser(mobile, username);
		List<ShoppingCart> list = shopService.findShoppingCarts(sid, user.getId());
		//定义保存购买总数量和总金额变量
		int totalNumber=0;
		for (ShoppingCart cart : list) {
			totalNumber += cart.getNumber();
		}
		return new StatusBean<Integer>(totalNumber, 200, "成功");
	}
	
	/**
	 * 清空购物车
	 * @param sid
	 * @param mobile
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "clear")
	public @ResponseBody Object clearCart(
			String sid,
			String mobile,
			HttpServletRequest request, HttpServletResponse response, Model model){
		//处理mobile参数
		if(StringUtils.isNotBlank(mobile))mobile = mobile.split(",")[0];
		ShoppingCart cart = new ShoppingCart();
		cart.setMerchant(new Merchant(sid));
		cart.setUser(UserUtils.getShopUser(mobile));
		shopService.clearShoppingCart(cart);
		return getStatusBean(200, "已成功清空购物车！");
	}
}
